/**
 * 打开一个外部链接
 * @param {*} objs
 *
 * url
 * title
 */
import objectAssign from "object-assign";
import { platform, alertTwoButt, settingPrivacy } from "../base";

export function openWebView(objs) {
	if (platform() == "web") {
		window.location.replace(objs.url);
	} else {
		return new Promise(function (resolve) {
			window.YHAuthorize.callHandler("Page_OpenWebView", objs, res => {
				resolve(res);
			});
		});
	}
}

/**
 * 打开一个外部链接
 * @param {*} objs
 *
 * url
 * title
 */
export function openModule(objs) {
	if (platform() == "web") {
		window.location.replace(objs.url);
	} else {
		return new Promise(function (resolve) {
			window.YHAuthorize.callHandler("Page_OpenModuleWebView", objs, res => {
				resolve(res);
			});
		});
	}
}

/**
 * 打开一个外部链接浏览器
 * @param {*} objs
 *
 * url
 * title
 */
export function openBrowser(url) {
	if (platform() == "web") {
		window.location.replace(url);
	} else {
		return new Promise(function (resolve) {
			window.YHAuthorize.callHandler("App_openBrowser", { url }, res => {
				resolve(res);
			});
		});
	}
}

/**
 * 打电话
 * @param objs
 * @returns {Promise<any>}
 */
export function callPhoneModule(objs) {
	if (platform() === "web") {
		return new Promise(function (resolve) {
			resolve();
		});
	} else {
		return new Promise(function (resolve, reject) {
			window.YHAuthorize.callHandler("Privacy_Call", objs, res => {
				if (res.status === "1000") {
					resolve(res);
				} else {
					reject(res);
				}
			});
		});
	}
}
/**
 * 打开地图地址选择
 * @param {*} objs
 *
 */
export function openSelectAddress(objs) {
	if (platform() == "web") {
		return new Promise(function (resolve) {
			resolve();
		});
	} else {
		return new Promise(function (resolve) {
			window.YHAuthorize.callHandler("Page_SelectAddress", objs, res => {
				resolve(res);
			});
		});
	}
}

/**
 * 选择联系人
 * @param {*} objs
 *
 */
export function openContactUI(objs) {
	if (platform() == "web") {
		return new Promise(function (resolve) {
			resolve();
		});
	} else {
		return new Promise(function (resolve) {
			window.YHAuthorize.callHandler("Privacy_ContactUI", objs, res => {
				resolve(res);
			});
		});
	}
}

/**
 * 获取通讯录
 * @param {*} objs
 *
 */
export function getContacts() {
	if (platform() === "web") {
		return new Promise(function (resolve) {
			resolve();
		});
	} else {
		return new Promise(function (resolve, reject) {
			window.YHAuthorize.callHandler("Privacy_Contacts", "", res => {
				if (res.status === "1000") {
					resolve(res);
				} else {
					reject(res);
					if (res.status == "3001") {
						let showMsg = "";
						if (platform() == "ios") {
							showMsg = `本应用暂时无法访问通讯录，请进入“设置-隐私-通讯录”进行授权。`;
						} else {
							showMsg = `暂时无法访问通讯录，请进入权限管理中授信本应用访问通讯录。`;
						}
						alertTwoButt(showMsg, "去设置", "取消", () => {
							settingPrivacy();
						});
					}
				}
			});
		});
	}
}

/**
 * 打开付款码
 * @param {*} objs
 *
 */
export function openPayView(objs) {
	if (platform() === "web") {
		return new Promise(function (resolve) {
			resolve();
		});
	} else {
		return new Promise(function (resolve) {
			window.YHAuthorize.callHandler("Page_OpenPayView", objs, res => {
				resolve(res);
			});
		});
	}
}
/**
 * 打开支付宝支付
 * @param {*} objs
 *
 */
export function openAlipayView(objs) {
	if (platform() === "web") {
		return new Promise(function (resolve) {
			resolve();
		});
	} else {
		return new Promise(function (resolve) {
			window.YHAuthorize.callHandler("Page_OpenAlipayView", objs, res => {
				resolve(res);
			});
		});
	}
}
/**
 * 打开支付宝还款
 * @param {*} objs
 *
 */
export function openRepaymentAlipayView(objs) {
	if (platform() === "web") {
		return new Promise(function (resolve) {
			resolve();
		});
	} else {
		return new Promise(function (resolve) {
			window.YHAuthorize.callHandler("Page_RepaymentAlipayView", objs, res => {
				resolve(res);
			});
		});
	}
}

/**
 * 关闭外部链接
 * @param {*} objs
 *
 * url
 * title
 */
export function closeModule(objs) {
	objs = objectAssign({}, objs, { _data: "none" });
	if (platform() === "web") {
		return;
	} else {
		return new Promise(function (resolve) {
			window.YHAuthorize.callHandler("SDK_ClosePage", objs, res => {
				resolve(res);
			});
		});
	}
}
